package com.yyk.yiliao.ui.trade.minepwd;

import android.os.CountDownTimer;
import com.google.android.material.textfield.TextInputEditText;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.bean.Sendsms_Info;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankRetrieveInfo;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 忘记密码
 */
public class FindInvestPasswordActivity extends BaseActivity {
    public static FindInvestPasswordActivity intance;

    @BindView(R.id.bt_next)
    Button btNext;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_sms)
    TextInputEditText tvSms;
    @BindView(R.id.tv_sendsms)
    TextView tvSendsms;
    private int cadeSms;
    private String phone;

    @Override
    public int getLayoutId() {
        return R.layout.activity_find_invest_password;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("找回交易密码");
    }

    @Override
    public void initData() {
        intance = this;
        //设置手机号码
        phone = Hawk.get("phone") + "";
        String newPHONE = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
        tvPhone.setText(newPHONE);
    }

    /**
     * 发送验证码
     */
    private TimeCount timeCount;

    /**
     * 设置 60秒时间 倒计时
     */
    private class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvSendsms.setClickable(false);
            tvSendsms.setText(millisUntilFinished / 1000 + "s后重发");
            tvSendsms.setTextColor(getResources().getColor(R.color.gray_c1));

        }

        @Override
        public void onFinish() {
            tvSendsms.setClickable(true);
            tvSendsms.setText("获取验证码");
            tvSendsms.setTextColor(getResources().getColor(R.color.blue_51));

        }

    }

    /**
     * 1.发送验证码
     * 2.找回交易密码
     *
     * @param view
     */
    @OnClick({R.id.tv_sendsms, R.id.bt_next})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.tv_sendsms:
                sendSms();
                break;
            case R.id.bt_next:
                sendNext();
                break;
        }
    }

    private void sendNext() {
        Map<String, String> map = new TreeMap<>();
        map.put("id", Hawk.get("uid") + "");
        map.put("code", tvSms.getText().toString().trim() + "");
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankRetrieve(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<UserbankRetrieveInfo>() {
                    @Override
                    public void onNext(UserbankRetrieveInfo info) {
                        Logger.e("找回交易密码验证手机" + info.toString());
                        if (info.getCode() == 1) {
                            startAct(SetInvestPasswordUpadteActivity.class);
                        } else {
                            ToastUtil.showShort(mActivity, info.getMsg());
                        }
                    }

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                });
    }

    private void sendSms() {

        Map<String, String> map = new TreeMap<>();
        map.put("telphone", phone);


        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postSendsms(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<Sendsms_Info>() {


                    @Override
                    public void onNext(Sendsms_Info info) {
                        Logger.e("发送验证码" + info.toString());
                        if (info.getCode() == 0) {
                            ToastUtil.showShort(mActivity, "短信获取次数过多");
                            return;
                        } else {
                            if (info.getCode() == 1) {
                                //获取到的验证码
                                cadeSms = info.getData();
                                Logger.i("手机验证码" + cadeSms + "");
                            }
                        }
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                });
        timeCount = new TimeCount(60000, 1000);
        timeCount.start();
    }


}
